﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TodoList.Mode;
using System.Xml.Linq;
using System.Configuration;
using FirstFloor.ModernUI.Windows.Controls;
using System.IO;
using TodoList.Properties;

namespace TodoList.Common
{
    public sealed class Utils
    {

        public static string DefaultFolderLocation
        {
            get
            {
                return string.Concat(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"\", "ModernTodo");
            }
        }

        public static List<Todo> GetTodoList(string FileName)
        {
            List<Todo> todoList = new List<Todo>();
            try
            {
                FileInfo finfo = new FileInfo(FileName);
                if (!finfo.Exists)
                {
                    //Directory.CreateDirectory(DefaultFolderLocation);                  
                    StreamWriter stm = new StreamWriter(FileName);
                    stm.WriteLine("<?xml version=\"1.0\"?><todo></todo>");
                    stm.Flush();
                    stm.Close();
                    SetLastFile(Settings.Default.LastFileItem, FileName);                                        
                }

                XDocument xTodoDoc = XDocument.Load(FileName);
                IEnumerable<XElement> xItems = xTodoDoc.Element("todo").Elements("todoitem");
                foreach (XElement xit in xItems)
                {
                    Todo todo = new Todo()
                    {
                        Id = xit.Attribute("Id").Value,
                        Title = xit.Attribute("Title").Value,
                        CompletedStatus = int.Parse(xit.Attribute("CompletedStatus").Value),
                        DueDate = xit.Attribute("DueDate").Value,
                        Priority = xit.Attribute("Priority").Value,
                        State = xit.Attribute("State").Value,
                        Description = xit.Attribute("Description").Value
                    };
                    todoList.Add(todo);
                }
            }
            catch (Exception ex)
            {
                ModernDialog errDialog = new ModernDialog();
                errDialog.Content = String.Format("Error! {0} ",ex.Message);
                errDialog.Title = "Todo List Error";
                errDialog.ShowDialog();
            }
            return todoList;
        }

        public static void SaveTodoItem(string FileName, List<Todo> todoList)
        {
            XElement xTodoMain = new XElement("todo");
            int iCount = 1;
            foreach (Todo item in todoList)
            {
                XElement xeleSubItem = new XElement("todoitem");
                xeleSubItem.SetAttributeValue("Id", iCount);
                xeleSubItem.SetAttributeValue("Title", item.Title);
                xeleSubItem.SetAttributeValue("CompletedStatus", item.CompletedStatus);
                xeleSubItem.SetAttributeValue("DueDate", item.DueDate);
                xeleSubItem.SetAttributeValue("Priority", item.Priority);
                xeleSubItem.SetAttributeValue("State", item.State);
                xeleSubItem.SetAttributeValue("Description", item.Description);

                xTodoMain.Add(xeleSubItem);
                iCount++;
            }

            xTodoMain.Save(FileName);
        }

        public static void SetLastFile(string key, string value)
        {
            string settingFile = string.Concat(DefaultFolderLocation, @"\", Settings.Default.SettingFile);
            StreamWriter stm = new StreamWriter(settingFile);
            string sRawXml = string.Format(Settings.Default.SettingContent , key, value);
            stm.WriteLine(sRawXml);
            stm.Flush();
            stm.Close();
            Console.WriteLine(sRawXml);
        }

        public static string GetLastFile(string key)
        {
            try
            {
                string settingFile = string.Concat(DefaultFolderLocation, @"\", Settings.Default.SettingFile);
                FileInfo finfo = new FileInfo(settingFile);
                string defaultFile = string.Concat(DefaultFolderLocation, @"\", Settings.Default.DefaultFileName);
                if (!finfo.Exists)
                {
                    SetLastFile(key, defaultFile);
                }
                XDocument xDoc = XDocument.Load(settingFile);
                XElement xItems = xDoc.Element("settings").Element("item");
                if (xItems == null)
                {
                    return defaultFile;
                }
                else
                {
                    return xItems.Attribute(Settings.Default.LastFileItem).Value;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return string.Empty;
            }

        }
    }
}
